from pylons import config

import logging
log = logging.getLogger(__name__)

class Plugin(object):
    tables = None
    configspec = ""

    def __init__(self, **options):
        pass
    
    def setup(self):
        self.createTables()

    def takedown(self):
        self.dropTables()

    def remove(self):
        pass

    def createTables(self):
        if not self.tables: 
            return
        log.info('Creating tables for %s...' % self)
        engine = config['pylons.g'].sa_engine
        for table in self.tables:
            log.info('creating table %s...' % table)
            table.create(bind=engine, checkfirst=True)

    def dropTables(self):
        if not self.tables:
            return
        log.info('Dropping tables for %s...' % self)
        engine = config['pylons.g'].sa_engine
        for table in self.tables:
            log.info('dropping table %s...' % table)
            table.drop(bind=engine, checkfirst=True)

    def addPermissions(self, thing, action_or_actions):
        if issubclass(type(action_or_actions), (tuple, list)):
            config['mtp.permissions'].update(action_or_actions)
        else:
            config['mtp.permissions'].add(action_or_actions)


